package com.gaotong.science.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.gaotong.science.entity.User;
import com.gaotong.science.service.UserService;
import com.gaotong.science.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.gaotong.science.config.MD5;


/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author suouyang
 * @since 2021-10-26
 */
@RestController
@RequestMapping("/user")
@CrossOrigin  //解决跨域
public class UserController {
    //login
    @Autowired
    UserService userService;
    @PostMapping("/login")
    public R login(String username, String password) {
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("username", username);
        System.out.println(username);
        System.out.println(password);
        User one = userService.getOne(wrapper);
//        System.out.println("**"+one.getPassword());
//        System.out.println("**"+one.getUsername());
        if (one != null && MD5.encrypt(password).equals(one.getPassword())) {
            return R.ok().data("token", "admin");
        }
        return R.error().message("登录失败,用户姓名或密码错误");
    }
    //info
    @GetMapping("info")
    public R info() {

        return R.ok().data("roles","[管理员]").data("name","admin").data("avatar","http://172.16.2.195:8003/static/1024f6-头像.png");
    }

    @PostMapping("/logout")
    public R logout(){

        return R.ok().data("token","");

    }




}
